package com.yzh.croom.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @GetMapping("/userLogin")
    public String userLogin(){
        return "login";
    }
    @GetMapping("/Logon")
    public String toLogon(){
        return "logon";
    }

    @GetMapping("/toWelCome")
    public String toWelCome(){
        return "welcome";
    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public void getUserInfo(){
        SecurityContext context = SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication = context.getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        System.out.println(principal);
        System.out.println("username:"+principal.getUsername());
        System.out.println("role:"+principal.getAuthorities());
    }
}
